{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 14, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>Vectors: One Dimensional Lists</h2>\n",
    "\n",
    "A <b>vector</b> is a list of numbers. \n",
    "\n",
    "Vectors are very useful to describe the state of a system, as you will see in the main tutorial in a concrete way. \n",
    "\n",
    "A list is a single object in python.\n",
    "\n",
    "Similarly, a vector is a single mathematical object. \n",
    "\n",
    "The number of elements in a list is its size or length.\n",
    "\n",
    "Similarly, the number of elements in a vector is called as the <b>size</b> or <b>dimension</b> of the vector."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# consider the following list with 4 elements \n",
    "L = [1,-2,0,5]\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Vectors can be in horizantal or vertical shape.\n",
    "\n",
    "We show this list as a <i><u>four dimensional</u></i> <b>row vector</b> (horizantial) or a <b>column vector</b> vertical():\n",
    "\n",
    "$$\n",
    "    u = \\mypar{1~~-2~~0~~-5} ~~~\\mbox{ or }~~~ v =\\mymatrix{r}{1 \\\\ -2 \\\\ 0 \\\\ 5}, ~~~\\mbox{ respectively.}\n",
    "$$\n",
    "\n",
    "Remark that we do not need to use any comma in vector representation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Multiplying a vector with a number</h3>\n",
    "\n",
    "A vector can be multiplied by a number.\n",
    "\n",
    "Multiplication of a vector with a number is also a vector: each entry is multiplied with this number.\n",
    "\n",
    "$$\n",
    "    3 \\cdot v = 3 \\cdot  \\mymatrix{r}{1 \\\\ -2 \\\\ 0 \\\\ 5} = \\mymatrix{r}{3 \\\\ -6 \\\\ 0 \\\\ 15}\n",
    "    ~~~~~~\\mbox{ or }~~~~~~\n",
    "    (-0.6) \\cdot v = (-0.6) \\cdot \\mymatrix{r}{1 \\\\ -2 \\\\ 0 \\\\ 5} = \\mymatrix{r}{-0.6 \\\\ 1.2 \\\\ 0 \\\\ -3}.\n",
    "$$\n",
    "\n",
    "You may consider this as enlarging or making smaller the vector.\n",
    "\n",
    "Let's verify our calculations in python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3 * v\n",
    "v = [1,-2,0,5]\n",
    "print(\"v is\",v)\n",
    "# we use the same list for the result\n",
    "for i in range(len(v)):\n",
    "    v[i] = 3 * v[i]\n",
    "print(\"3v is\",v)\n",
    "\n",
    "# -0.6 * u\n",
    "# reinitialize the list v\n",
    "v = [1,-2,0,5]\n",
    "for i in range(len(v)):\n",
    "    v[i] = -0.6 * v[i]\n",
    "print(\"0.6v is\",v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Summation of vectors</h3>\n",
    "\n",
    "Two vectors can be summed up.\n",
    "\n",
    "The summation of two vectors is a vector: the numbers in the same entries are added up.\n",
    "\n",
    "$$\n",
    "    u = \\myrvector{-3 \\\\ -2 \\\\ 0 \\\\ -1 \\\\ 4} \\mbox{ and } v = \\myrvector{-1\\\\ -1 \\\\2 \\\\ -3 \\\\ 5}.\n",
    "    ~~~~~~~ \\mbox{Then, }~~\n",
    "    u+v = \\myrvector{-3 \\\\ -2 \\\\ 0 \\\\ -1 \\\\ 4} + \\myrvector{-1\\\\ -1 \\\\2 \\\\ -3 \\\\ 5} =\n",
    "    \\myrvector{-3+(-1)\\\\ -2+(-1) \\\\0+2 \\\\ -1+(-3) \\\\ 4+5} = \\myrvector{-4\\\\ -3 \\\\2 \\\\ -4 \\\\ 9}.\n",
    "$$\n",
    "\n",
    "Let's do the same calculations in python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "u = [-3,-2,0,-1,4]\n",
    "v = [-1,-1,2,-3,5]\n",
    "result=[]\n",
    "for i in range(len(u)):\n",
    "    result.append(u[i]+v[i])\n",
    "\n",
    "print(\"u+v is\",result)\n",
    "\n",
    "# let's also print the result vector similar to a column vector\n",
    "print() # print an empty line\n",
    "print(\"the elements of u+v are\")\n",
    "for j in range(len(result)):\n",
    "    print(result[j])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 1 </h3>\n",
    "\n",
    "Create two 7-dimensional vectors $u$ and $ v $ as a list in python having entries randomly picked between $-10$ and $10$. \n",
    "\n",
    "Print their entries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "#\n",
    "# your solution is here\n",
    "#\n",
    "\n",
    "#r=randrange(-10,11) # randomly pick a number from the list {-10,-9,...,-1,0,1,...,9,10}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B16_Python_Lists_Vectors_Solutions.ipynb#task1\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 2 </h3>\n",
    "\n",
    "By using the same vectors, find the vector $  (3  u-2  v) $ and print its entries. Here $ 3u $ and $ 2v $ means $u$ and $v$ are multiplied by $3$ and $2$, respectively."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B16_Python_Lists_Vectors_Solutions.ipynb#task2\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Visualization of vectors </h3>\n",
    "\n",
    "We can visualize the vectors with dimension at most 3. \n",
    "\n",
    "For simplicity, we give examples of 2-dimensional vectors. \n",
    "\n",
    "Consider the vector $ v = \\myvector{1 \\\\ 2} $. \n",
    "\n",
    "A 2-dimensional vector can be represented on the two-dimensional plane by an arrow starting from the origin $ (0,0) $ to the point $ (1,2) $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"../images/vector_1_2-small.jpg\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's represent the vectors $ 2v = \\myvector{2 \\\\ 4} $ and $ -v = \\myvector{-1 \\\\ -2} $.\n",
    "\n",
    "<img src=\"../images/vectors_2_4_-1_-2.jpg\" width=\"40%\">\n",
    "\n",
    "As you can observe, after multiplying by 2, the vector is enlarged, and, after multiplying by $(-1)$, the vector is the same but its direction is opposite."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> The length of a vector </h3>\n",
    "\n",
    "The length of a vector is the (shortest) distance from the points <b>pointed</b> by the elements of vector to the origin point $(0,0)$.\n",
    "\n",
    "The length of a vector can be calculated by using Pythagoras Theorem. \n",
    "\n",
    "We visualize the vector, its length, and the contributions of each entry in the vector to the length. \n",
    "\n",
    "Consider the vector $ u = \\myrvector{-3 \\\\ 4} $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"../images/length_-3_4-small.jpg\" width=\"80%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The length of $ u $ is denoted as $ \\norm{u} $, and it is calculated as $ \\norm{u} =\\sqrt{(-3)^2+4^2} = 5 $. \n",
    "\n",
    "Here each entry is contributed with its square value. All contributions are summed up. Thus we obtain the square of the length. \n",
    "\n",
    "This formula is generalized to any dimension. \n",
    "\n",
    "Let's find the length of the following vector by using python:\n",
    " \n",
    "$$\n",
    "    v = \\myrvector{-1 \\\\ -3 \\\\ 5 \\\\ 3 \\\\ 1 \\\\ 2}\n",
    "    ~~~~~~~~~~\n",
    "    \\mbox{and}\n",
    "    ~~~~~~~~~~\n",
    "    \\norm{v} = \\sqrt{(-1)^2+(-3)^2+5^2+3^2+1^2+2^2} .\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<i> Hint: There is a short way of writing power operation in python. \n",
    "    <ul>\n",
    "        <li> In its generic form: $ a^x $ can be denoted as $ a ** x $ in python. </li>\n",
    "        <li> The square of a number $a$: $ a^2 $ can be denoted as $ a ** 2 $ in python. </li>\n",
    "        <li> The square root of a number $ a $: $ \\sqrt{a} = a^{\\frac{1}{2}} = a^{0.5} $ can be denoted as $ a ** 0.5 $ in python.</li>\n",
    "    </ul>\n",
    "</i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = [-1,-3,5,3,1,2]\n",
    "\n",
    "length_square=0\n",
    "for i in range(len(v)):\n",
    "    print(v[i],\":square ->\",v[i]**2) # let's print each entry and its square value\n",
    "    length_square = length_square + v[i]**2 # let's sum up the square of each entry\n",
    "\n",
    "length = length_square ** 0.5 # let's take the square root of the summation of the squares of all entries\n",
    "print(\"the summation is\",length_square)\n",
    "print(\"then the length is\",length)\n",
    "\n",
    "# for square root, we can also use built-in function math.sqrt\n",
    "print() # print an empty line\n",
    "from math import sqrt\n",
    "print(\"the sqaure root of\",length_square,\"is\",sqrt(length_square))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 3 </h3>\n",
    "\n",
    "Let $ u = \\myrvector{1 \\\\ -2 \\\\ -4 \\\\ 2} $ be a four dimensional vector.\n",
    "\n",
    "Verify that $ \\norm{4 u} = 4 \\cdot \\norm{u} $ in python. \n",
    "\n",
    "Remark that $ 4u $ is another vector obtained from $ u $ by multiplying it with 4. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B16_Python_Lists_Vectors_Solutions.ipynb#task3\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b> Notes:</b>\n",
    "\n",
    "When a vector is multiply by a number, then its length is also multiplied with the same number.\n",
    "\n",
    "But, we should be careful with the sign.\n",
    "\n",
    "Consider the vector $ -3 v $. It has the same length of $ 3v $, but its direction is opposite.\n",
    "\n",
    "So, when calculating the length of $ -3 v $, we use absolute value of the number:\n",
    "\n",
    "$ \\norm{-3 v} = |-3| \\norm{v} = 3 \\norm{v}  $.\n",
    "\n",
    "Here $ |-3| $ is the absolute value of $ -3 $. \n",
    "\n",
    "The absolute value of a number is its distance to 0. So, $ |-3| = 3 $.\n",
    "\n",
    "<i> Remark that length cannot be $ negative $ :) </i>."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 4 </h3>\n",
    "\n",
    "Let $ u = \\myrvector{1 \\\\ -2 \\\\ -4 \\\\ 2} $ be a four dimensional vector.\n",
    "\n",
    "Randomly pick a number $r$ from $ \\left\\{ \\dfrac{1}{10}, \\dfrac{2}{10}, \\cdots, \\dfrac{9}{10} \\right\\} $.\n",
    "\n",
    "Find the vector $(-r)\\cdot u$ and then its length."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B16_Python_Lists_Vectors_Solutions.ipynb#task4\">click for our solution</a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
